package com.qiyou.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.qiyou.dao.IBlogCommentDao;
import com.qiyou.dao.IBlogDao;
import com.qiyou.dao.factory.DaoFactory;
import com.qiyou.entity.Blog;
import com.qiyou.entity.BlogComment;
import com.qiyou.util.LogUtil;

/**
 * 全部博客信息
 * 
 * @author 杨强
 */
@WebServlet("/CommentListService")
public class CommentListService extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final Logger log = LogUtil
            .getLogger(CommentListService.class);

    public CommentListService() {
        super();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");

        String blogIDStr = request.getParameter("id");
        IBlogDao blogDao = DaoFactory.getBlogDao();
        try {
            Blog blog = blogDao.getBlog(Long.parseLong(blogIDStr));
            if (blog != null) {
                blog.setBlogNums(blog.getBlogNums() + 1);
            }
            blogDao = DaoFactory.getBlogDao();
            blogDao.doUpdate(blog);
        } catch (NumberFormatException e) {
            log.error(e);
        } catch (SQLException e) {
            log.error(e);
        }
        IBlogCommentDao dao = DaoFactory.getBlogCommentDao();

        List<BlogComment> comments = null;
        try {
            comments = dao.getBlogComments(Long.parseLong(blogIDStr));
        } catch (NumberFormatException e) {
            log.error("类型转换错误：" + blogIDStr, e);
        } catch (SQLException e) {
            log.error(e);
        }
        TreeMap<Long, BlogComment> resultMap = new TreeMap<Long, BlogComment>();
        if (comments != null && !comments.isEmpty()) {
            for (BlogComment blogComment : comments) {
                resultMap.put(blogComment.getID(), blogComment);
            }
        }
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create();
        // List<TreeMap<Long, BlogComment>> result = new ArrayList<TreeMap<Long,
        // BlogComment>>();
        // if (!resultMap.isEmpty()) {
        // result.add(resultMap);
        // }
        String msg = gson.toJson(resultMap);
        PrintWriter out = response.getWriter();
        out.print(msg);
        out.flush();
        out.close();

        log.info("用户:" + request.getRemoteAddr() + "访问了博客:" + blogIDStr
                + ",评论信息:" + msg);
    }

}
